package com.fruit.service.order.impl;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.fruit.constants.AfterSalesServiceConstant;
import com.fruit.constants.GoodsConstant;
import com.fruit.constants.OrderConstant;
import com.fruit.mapper.goods.GoodsMapper;
import com.fruit.mapper.order.DshOrderMapper;
import com.fruit.mapper.order.OrderMapper;
import com.fruit.mapper.pay.PayMapper;
import com.fruit.mapper.refund.AfterSalesServiceMapper;
import com.fruit.mapper.shopping.ShoppingMapper;
import com.fruit.pojo.common.OperatingConfig;
import com.fruit.pojo.goods.Goods;
import com.fruit.pojo.goods.GoodsTradePrice;
import com.fruit.pojo.goods.Supplier;
import com.fruit.pojo.order.DSHOrder;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.order.OrderConfig;
import com.fruit.pojo.order.OrderGoods;
import com.fruit.pojo.refund.AfterSalesService;
import com.fruit.pojo.shopping.Shopping;
import com.fruit.service.order.OrderConfirmManager;
import com.fruit.service.shopping.ShoppingManager;
import com.fruit.util.CustomUtil;
import com.fruit.util.DataUtil;
import com.fruit.util.DateUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;


/** 订单
 * 修改时间：2016.07.15
 */
@Service("orderConfirmService")
public class OrderConfirmService implements OrderConfirmManager{

	@Autowired
	private ShoppingMapper shoppingMapper;
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Autowired
	private ShoppingManager shoppingService;
	
	@Autowired
	private DshOrderMapper dshOrderMapper;
	
	@Autowired
	private OrderMapper orderMapper;
	
	@Autowired
	private AfterSalesServiceMapper afterSalesServiceMapper;
	
	@Autowired
	private PayMapper payMapper;
	
	@Autowired
    private DelayService delayService;  

	@Override
	public Map<String, Object> createValicationShopping(Shopping sp) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		if(sp.getIds() == null || sp.getIds().length == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
			return resMap;
		}
		
		List<Shopping> shoppingList = shoppingMapper.getShoppingListByIds(sp);
		if(shoppingList == null || shoppingList.size() == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "shopping_empty", "购物车为空!"));
			return resMap;
		}
		
		List<OrderConfig> orderConfigList = dshOrderMapper.getOrderConfig();
		if(orderConfigList == null || orderConfigList.size() != 1){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception_order_config", "系统繁忙!"));
			return resMap;
		}
		
		OrderConfig orderConfig = orderConfigList.get(0);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", sp.getUserId());
		params.put("state", OrderConstant.PAY_NO);
		
		int orderCancelCnt = orderMapper.getOrderCntByUserId(params);
		if(orderCancelCnt >= orderConfig.getOrderNotpayMaxNum()){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_cancel_max", "未支付订单数量达到上限，请先完成支付!"));
			return resMap;
		}
		
		List<Long> advanceList = new ArrayList<Long>();
		List<Long> sportList = new ArrayList<Long>();
		BigDecimal advancePrice = new BigDecimal(0);
		BigDecimal sportPrice = new BigDecimal(0);
		for(Shopping shopping:shoppingList){
			Goods goods = goodsMapper.getGoodsDetail(shopping.getGoodsId());
			if(goods == null){
				//删除购物车
				long ids[] = {shopping.getId()};
				shopping.setIds(ids);
				shoppingService.delShopping(shopping);
				continue;
			}
			//验证商品是否已下架
			if(GoodsConstant.STATE_DOWN.equals(goods.getState())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）已下架!"));
				return resMap;
			}
			
			String curTime = DateUtil.getTime();
			if(!(DateUtil.compareDate2(curTime,goods.getStartTime()) && DateUtil.compareDate2(goods.getEndTime(),curTime))){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_no_exists", "商品（"+goods.getTitle()+"）未上架或已下架!"));
				return resMap;
			}	
			
			List<OperatingConfig> ocList = payMapper.getOperatingConfig();
			if(ocList != null && ocList.size() ==1){
				OperatingConfig oc = ocList.get(0);
				long hsmTime = DateUtil.getHsmTime();
				
				if(hsmTime < oc.getMinTime() || hsmTime > oc.getMaxTime()){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "operating_error", "已过运营时间!"));
					return resMap;
				}

			}
			
			
			//商品单位换算
			if(!StringUtils.equals(shopping.getUnit(), goods.getUnit())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "unit_error", "单位错误!"));
				return resMap;
			}
			int unitNum = shopping.getNum();
			//验证库存
			if(goods.getStockNum() < unitNum){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）库存不足"));
				return resMap;
			}
			
			if(shopping.getNum() < goods.getMinShopNum()){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）起订量为"+goods.getMinShopNum()+goods.getUnitDesc()));
				return resMap;
			}
			
			//验证预售商品是否到期
			if(GoodsConstant.ADVANCE_Y.equals(goods.getIsAdvanceState())){
				String advanceStartTime = goods.getAdvanceStartTime();
				Date advanceStartTimeD = DateUtil.fomatDateTime(advanceStartTime, "yyyy-MM-dd HH:mm:ss");
				//验证开始时间
				if(DateUtil.subtractTime(advanceStartTimeD,new Date()) > 0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）预售未开始"));
					return resMap;
				}
				String advanceEndTime = goods.getAdvanceEndTime();
				Date advanceEndTimeD = DateUtil.fomatDateTime(advanceEndTime, "yyyy-MM-dd HH:mm:ss");
				//验证结束时间
				if(DateUtil.subtractTime(advanceEndTimeD, new Date()) <= 0){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）预售已结束"));
					return resMap;
				}
			}
			
			//计算批发价
			params.clear();
			params.put("goodsId", goods.getId());
			params.put("num", shopping.getNum());
			List<GoodsTradePrice> gtpList = goodsMapper.getGoodsTradePrice(params);
			if(gtpList != null && gtpList.size() == 1){
				GoodsTradePrice gtp = gtpList.get(0);
				goods.setTradePrice(gtp.getPrice());
			}else{
				//删除购物车
				long ids[] = {shopping.getId()};
				shopping.setIds(ids);
				shoppingService.delShopping(shopping);
				continue;
			}

			if(GoodsConstant.ADVANCE_Y.equals(goods.getIsAdvanceState())){
				
				advanceList.add(shopping.getId());
				advancePrice = advancePrice.add(goods.getTradePrice().multiply(new BigDecimal(shopping.getNum())));
			}else{
				sportList.add(shopping.getId());
				sportPrice = sportPrice.add(goods.getTradePrice().multiply(new BigDecimal(shopping.getNum())));
			}
			
		}
		resMap.put("advanceSize", advanceList.size());
		resMap.put("advancePrice", advancePrice);
		resMap.put("advanceType", "01");
		resMap.put("advanceList", advanceList);
		
		
		resMap.put("sportSize", sportList.size());
		resMap.put("sportPrice", sportPrice);
		resMap.put("sportType", "00");
		resMap.put("sportList", sportList);
		
		resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "验证成功"));
		return resMap;

	}

	@Override
	public Map<String, Object> getConfirmManager(Shopping sp) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		if(sp.getIds() == null || sp.getIds().length == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
			return resMap;			
		}
		
		if(!GoodsConstant.ADVANCE_Y.equals(sp.getGoodsType()) && !GoodsConstant.ADVANCE_N.equals(sp.getGoodsType())){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
			return resMap;			
		}
		
		List<Shopping> shoppingList = shoppingMapper.getShoppingListByIds(sp);
		if(shoppingList == null || shoppingList.size() == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "shopping_empty", "购物车为空!"));
			return resMap;
		}
		
		List<OrderConfig> orderConfigList = dshOrderMapper.getOrderConfig();
		if(orderConfigList == null || orderConfigList.size() != 1){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception_order_config", "系统繁忙!"));
			return resMap;
		}
		
		OrderConfig orderConfig = orderConfigList.get(0);
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userId", sp.getUserId());
		params.put("state", OrderConstant.PAY_NO);
		
		int orderCancelCnt = orderMapper.getOrderCntByUserId(params);
		if(orderCancelCnt >= orderConfig.getOrderNotpayMaxNum()){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_cancel_max", "未支付订单数量达到上限，请先完成支付!"));
			return resMap;
		}
		
		if(GoodsConstant.ADVANCE_Y.equals(sp.getGoodsType())){
			resMap = getOrderListTypeA(shoppingList);
		}else{
			resMap = getOrderListTypeS(shoppingList);
		}
		if(resMap.containsKey("result")){
			return resMap;
		}
		//查询默认收货地址
		resMap.put("address", goodsMapper.getDefaultAddress(sp.getUserId()));
		resMap.put("platfromName", "水果采购平台");
		resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "成功!"));
		return resMap;
	}
	
	public Map<String, Object> getOrderListTypeA(List<Shopping> shoppingList){
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<Order> orderList = new ArrayList<Order>();
		List<String> keyList = new ArrayList<String>();
		BigDecimal allTotalPrice = new BigDecimal(0);
		for(Shopping shopping:shoppingList){
			Goods goods = goodsMapper.getGoodsDetail(shopping.getGoodsId());
			if(goods == null){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			
			if(!GoodsConstant.ADVANCE_Y.equals(goods.getIsAdvanceState())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			//验证商品是否已下架
			if(GoodsConstant.STATE_DOWN.equals(goods.getState())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）已下架"));
				return resMap;
			}
			
			String curTime = DateUtil.getTime();
			if(!(DateUtil.compareDate2(curTime,goods.getStartTime()) && DateUtil.compareDate2(goods.getEndTime(),curTime))){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_no_exists", "商品未上架或已下架!"));
				return resMap;
			}	
			
			List<OperatingConfig> ocList = payMapper.getOperatingConfig();
			if(ocList != null && ocList.size() ==1){
				OperatingConfig oc = ocList.get(0);
				long hsmTime = DateUtil.getHsmTime();
				
				if(hsmTime < oc.getMinTime() || hsmTime > oc.getMaxTime()){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "operating_error", "已过运营时间!"));
					return resMap;
				}

			}
			
			//商品单位换算
			if(!StringUtils.equals(shopping.getUnit(), goods.getUnit())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "unit_error", "单位错误!"));
				return resMap;
			}
			int unitNum = shopping.getNum();
			//验证库存
			if(goods.getStockNum() < unitNum){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）库存不足"));
				return resMap;
			}
			
			if(shopping.getNum() < goods.getMinShopNum()){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）起订量为"+goods.getMinShopNum()+goods.getUnitDesc()));
				return resMap;
			}
			
			//验证预售商品是否到期
			String advanceStartTime = goods.getAdvanceStartTime();
			Date advanceStartTimeD = DateUtil.fomatDateTime(advanceStartTime, "yyyy-MM-dd HH:mm:ss");
			//验证开始时间
			if(DateUtil.subtractTime(advanceStartTimeD,new Date()) > 0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）预售未开始"));
				return resMap;
			}
			String advanceEndTime = goods.getAdvanceEndTime();
			Date advanceEndTimeD = DateUtil.fomatDateTime(advanceEndTime, "yyyy-MM-dd HH:mm:ss");
			//验证结束时间
			if(DateUtil.subtractTime(advanceEndTimeD, new Date()) <= 0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）预售已结束"));
				return resMap;
			}
			//计算批发价
			Map<String, Object> params = new HashMap<String, Object>();
			params.clear();
			params.put("goodsId", goods.getId());
			params.put("num", shopping.getNum());
			List<GoodsTradePrice> gtpList = goodsMapper.getGoodsTradePrice(params);
			if(gtpList != null && gtpList.size() == 1){
				GoodsTradePrice gtp = gtpList.get(0);
				goods.setTradePrice(gtp.getPrice());
			}else{
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			//商品信息
			OrderGoods orderGoods = new OrderGoods();
			orderGoods.setGoodsId(goods.getId());
			orderGoods.setTitle(goods.getTitle());
			orderGoods.setGoodsThumb(goods.getThumb());
			orderGoods.setNumber(shopping.getNum());
			orderGoods.setTradePrice(goods.getTradePrice());
			

			//运费 待定
			Order order = new Order();
			//查询供货商
			Supplier s = goodsMapper.getSupplierById(goods.getSupplier());
			if(s != null){
				order.setSupplierName(s.getName());
			}
			order.setTransitPrice(new BigDecimal(0));
			order.setTotalPrice(orderGoods.getTradePrice().multiply(DataUtil.getBigDecimal(orderGoods.getNumber())));
			order.setReservePrice(order.getTotalPrice().multiply(DataUtil.getBigDecimal("0.3")).setScale(2, BigDecimal.ROUND_HALF_UP));
			order.setFinalPrice(order.getTotalPrice().subtract(order.getReservePrice()));
			order.getOrderGoods().add(orderGoods);
			orderList.add(order);
			keyList.add(String.valueOf(goods.getId()));
			allTotalPrice = allTotalPrice.add(order.getTotalPrice());
		}
		resMap.put("orderList", orderList);
		resMap.put("keyList", keyList);
		resMap.put("allTotalPrice", allTotalPrice);
		resMap.put("goodsType", GoodsConstant.ADVANCE_N);
		//支付方式
		List<Map<String,String>> payMode = new ArrayList<Map<String,String>>();
		Map<String, String> payModeMap = new HashMap<String, String>();
		payModeMap.put("TRANSFER", "公司转账");
		payMode.add(payModeMap);
		
		//支付类型
		List<Map<String,String>> payType = new ArrayList<Map<String,String>>();
		Map<String, String> payTypeMap = new HashMap<String, String>();
		payTypeMap.put("ALL", "全款");
		payType.add(payTypeMap);
		resMap.put("payMode", payMode);
		resMap.put("payType", payType);
		return resMap;
		
	}

	public Map<String, Object> getOrderListTypeS(List<Shopping> shoppingList){
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<Order> orderList = new ArrayList<Order>();
		List<String> keyList = new ArrayList<String>();
		BigDecimal allTotalPrice = new BigDecimal(0);
		Map<String, Order> orderMap= new HashMap<String, Order>();
		for(Shopping shopping:shoppingList){
			Goods goods = goodsMapper.getGoodsDetail(shopping.getGoodsId());
			if(goods == null){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			
			if(!GoodsConstant.ADVANCE_N.equals(goods.getIsAdvanceState())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			//验证商品是否已下架
			if(GoodsConstant.STATE_DOWN.equals(goods.getState())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）已下架"));
				return resMap;
			}
			
			String curTime = DateUtil.getTime();
			if(!(DateUtil.compareDate2(curTime,goods.getStartTime()) && DateUtil.compareDate2(goods.getEndTime(),curTime))){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_no_exists", "商品（"+goods.getTitle()+"）未上架或已下架!"));
				return resMap;
			}		
			
			List<OperatingConfig> ocList = payMapper.getOperatingConfig();
			if(ocList != null && ocList.size() ==1){
				OperatingConfig oc = ocList.get(0);
				long hsmTime = DateUtil.getHsmTime();
				
				if(hsmTime < oc.getMinTime() || hsmTime > oc.getMaxTime()){
					resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "operating_error", "已过运营时间!"));
					return resMap;
				}

			}	
			
			//验证商品
			if(goods.getSupplier() == 0){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）参数错误"));
				return resMap;
			}
			
			//商品单位换算
			if(!StringUtils.equals(shopping.getUnit(), goods.getUnit())){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "unit_error", "单位错误!"));
				return resMap;
			}
			int unitNum = shopping.getNum();
			//验证库存
			if(unitNum < goods.getMinShopNum()){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）起订量为"+goods.getMinShopNum()+goods.getUnitDesc()));
				return resMap;
			}
			
			if(goods.getStockNum() < unitNum){
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "goods_error", "商品（"+goods.getTitle()+"）库存不足"));
				return resMap;
			}
			
			//计算批发价
			Map<String, Object> params = new HashMap<String, Object>();
			params.clear();
			params.put("goodsId", goods.getId());
			params.put("num", shopping.getNum());
			List<GoodsTradePrice> gtpList = goodsMapper.getGoodsTradePrice(params);
			if(gtpList != null && gtpList.size() == 1){
				GoodsTradePrice gtp = gtpList.get(0);
				goods.setTradePrice(gtp.getPrice());
			}else{
				resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "params_error", "参数错误!"));
				return resMap;
			}
			
			//商品信息
			OrderGoods orderGoods = new OrderGoods();
			orderGoods.setGoodsId(goods.getId());
			orderGoods.setTitle(goods.getTitle());
			orderGoods.setGoodsThumb(goods.getThumb());
			orderGoods.setNumber(shopping.getNum());
			orderGoods.setTradePrice(goods.getTradePrice());
			//判断平台是否存在
			String key = String.valueOf(goods.getSupplier());
			Order order = null;
			if(orderMap.containsKey(key)){
				order = orderMap.get(key);
			}else{
				order = new Order();
				keyList.add(key);
			}
			//查询平台
			Supplier s = goodsMapper.getSupplierById(goods.getSupplier());
			if(s != null){
				order.setSupplierName(s.getName());
			}

			//运费 待定
			order.setTransitPrice(new BigDecimal(0));
			order.setTotalPrice(DataUtil.getBigDecimal(order.getTotalPrice()).add(orderGoods.getTradePrice().multiply(DataUtil.getBigDecimal(orderGoods.getNumber()))));
			order.setReservePrice(order.getTotalPrice().multiply(DataUtil.getBigDecimal("0.3")).setScale(2, BigDecimal.ROUND_HALF_UP));
			order.setFinalPrice(order.getTotalPrice().subtract(order.getReservePrice()));
			order.getOrderGoods().add(orderGoods);
			orderMap.put(key, order);
			allTotalPrice = allTotalPrice.add(order.getTotalPrice());
		}
		orderList.addAll(orderMap.values());	
		resMap.put("keyList", keyList);
		resMap.put("orderList", orderList);
		resMap.put("allTotalPrice", allTotalPrice);
		resMap.put("goodsType", GoodsConstant.ADVANCE_N);
		//支付方式
		List<Map<String,String>> payMode = new ArrayList<Map<String,String>>();
		Map<String, String> payModeMap = new HashMap<String, String>();
		payModeMap.put("TRANSFER", "公司转账");
		payMode.add(payModeMap);
		
		//支付类型
		List<Map<String,String>> payType = new ArrayList<Map<String,String>>();
		Map<String, String> payTypeMap = new HashMap<String, String>();
		payTypeMap.put("ALL", "全款");
		payType.add(payTypeMap);
		resMap.put("payMode", payMode);
		resMap.put("payType", payType);
		return resMap;
		
	}


	@Override
	public Map<String, Object> updateAutoCancelOrder(DSHOrder dshOrder) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
			
		DSHOrder ndshOrder = dshOrderMapper.getDshOrderByOrderId(dshOrder);
		 if(ndshOrder == null){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "dsh_not_exists", null));
			return resMap;
			 
		 }
		 Order order = dshOrderMapper.getOrder(ndshOrder.getOrderId());
		 if(order == null){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "dsh_not_exists", null));
			return resMap;
		 }
		 
		 if(!OrderConstant.PAY_NO.equals(order.getState())){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "order_not_pay", null));
			dshOrderMapper.delDshOrderById(ndshOrder.getId());
			return resMap;
		 }
		 order.setState(OrderConstant.CANCEL);
		 int updateNum = dshOrderMapper.updateOrderState(order);
		 if(updateNum == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "again_start", null));
			return resMap;
		 }
		 
		 AfterSalesService as = new AfterSalesService();
		 as.setCodePre(DateUtil.getYear());
		 as.setCodeSuf(CustomUtil.codeSuf(3, order.getUserId()));
		 as.setOrderCode(order.getCodePre()+order.getId()+order.getCodeSuf());
		 as.setOrderId(order.getId());
		 as.setUserId(order.getUserId());
		 as.setIsAuto(AfterSalesServiceConstant.AUTO);
		 as.setType(AfterSalesServiceConstant.TYPE_CANCEL);
		 as.setState(AfterSalesServiceConstant.STATE_FINISH);
		 as.setCreateTime(DateUtil.getTime());
		 
		 int num = afterSalesServiceMapper.saveSalesService(as);
		 
		 if(num == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "again_start", null));
			return resMap;
		 }
		 
		 int delNum = dshOrderMapper.delDshOrderById(ndshOrder.getId());
		 if(delNum == 0){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "roll_back", null,null,ResultInfo.ROLLBACK));
			return resMap;
		 }
		 //恢复商品库存
		 List<OrderGoods> ogList = orderMapper.getOrderGoodsList(order);
		 if(ogList != null){
			 for(OrderGoods og : ogList){
				 payMapper.updateAddGoodsStockNum(og);
			 }
		 }
		 
		  
		 resMap.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "ok", "成功!"));
		 return resMap;
		}catch(Exception e){
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", null,null,ResultInfo.ROLLBACK));
			return resMap;
		}finally{
			ResultInfo ri = (ResultInfo)resMap.get("result");
			if(ResultInfo.ROLLBACK.equals(ri.getIsRollBack())){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}
		
	}

	

	@Override
	public void newSaveAutoCancelOrder(final long orderId) {
		try{
		int timeOut = 7200;
		List<OrderConfig> oc = dshOrderMapper.getOrderConfig();
		if(oc != null && oc.size() == 1){
				timeOut = oc.get(0).getAutoCancelTime();
		}	
			
		DSHOrder order = new DSHOrder(orderId,OrderConstant.AUTO_CANCEL,timeOut);
		
		int num = dshOrderMapper.insertDshOrder(order);
		if(num == 1){
			delayService.add(order);
		}
		}catch(Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		
	}


	@Override
	public List<DSHOrder> getDshOrderList(DSHOrder dshOrder) {
		return dshOrderMapper.getDshOrderList(dshOrder);
	}
	
	
	
	
}
